/*
 * @Descripttion: 初始化配置信息
 * @Author: liuxiaoling
 * @Date: 2021-03-28 16:07:27
 * @LastEditors: liuxiaoling
 * @LastEditTime: 2021-04-03 22:49:16
 */
import api from '../api'
import constant from '../utils/constant'
import positionIdList from '../constants/positionIdList'
import classAttributeIdList from '../constants/classAttributeIdList'
import benefitsList from '../constants/benefitsList'
import {payMoneyMode} from '../constants/payMoneyMode'
import paydayIdList from '../constants/paydayIdList'

import {Toast} from 'vant'

/** 初始化配置数据 */
export function initConfig() {
  getClassproperties()

  getJobattributes()

  getBenefits()

  getPaymodes()

  getPaydays()
}

/** 班制属性 */
function getClassproperties() {
  api.config
    .getClassproperties()
    .then(res => {
      if (res.code == 200 && res.data) {
        localStorage.setItem(constant.STORAGE_CLASSPROPERTIES, JSON.stringify(res.data))
      } else {
        localStorage.setItem(constant.STORAGE_CLASSPROPERTIES, JSON.stringify(positionIdList))
      }
    })
    .catch(error => {
      Toast('服务器连接失败!')
      console.log(error)
    })
}

/** 职位属性 */
function getJobattributes() {
  api.config
    .getJobattributes()
    .then(res => {
      if (res.code == 200 && res.data) {
        localStorage.setItem(constant.STORAGE_JOBATTRIBUTES, JSON.stringify(res.data))
      } else {
        localStorage.setItem(constant.STORAGE_JOBATTRIBUTES, JSON.stringify(classAttributeIdList))
      }
    })
    .catch(error => {
      Toast('服务器连接失败!')
      console.log(error)
    })
}

/** 福利属性 */
function getBenefits() {
  api.config
    .getBenefits()
    .then(res => {
      if (res.code == 200 && res.data) {
        localStorage.setItem(constant.STORAGE_BENEFITS, JSON.stringify(res.data))
      } else {
        localStorage.setItem(constant.STORAGE_BENEFITS, JSON.stringify(benefitsList))
      }
    })
    .catch(error => {
      Toast('服务器连接失败!')
      console.log(error)
    })
}

/** 算薪方式 */
function getPaymodes() {
  api.config
    .getPaymodes()
    .then(res => {
      if (res.code == 200 && res.data) {
        localStorage.setItem(constant.STORAGE_PAYMODES, JSON.stringify(res.data))
      } else {
        localStorage.setItem(constant.STORAGE_PAYMODES, JSON.stringify(payMoneyMode))
      }
    })
    .catch(error => {
      Toast('服务器连接失败!')
      console.log(error)
    })
}

/** 发薪日 */
function getPaydays() {
  api.config
    .getPaydays()
    .then(res => {
      if (res.code == 200 && res.data) {
        localStorage.setItem(constant.STORAGE_PAYDAYS, JSON.stringify(res.data))
      } else {
        localStorage.setItem(constant.STORAGE_PAYDAYS, JSON.stringify(paydayIdList))
      }
    })
    .catch(error => {
      Toast('服务器连接失败!')
      console.log(error)
    })
}
